package com.leador.batch.db;



import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lhy on 2016/7/12.
 */
public class LogUtil {
    private static LogUtil INSTANCE = null;
    private LogUtil(){

    }
    public static LogUtil getInstance(){
        synchronized (LogUtil.class){
            if(INSTANCE == null){
             INSTANCE = new LogUtil();
            }
        }
        return INSTANCE;
    }

    public String getExcelPaht(){
        return PackageUtils.getCacheDirectory();
    }

    public String getCachePath(){
        return PackageUtils.getCacheDirectory()+"/cache.txt";
    }

    public void writeLog(String log){
        FileWriter fw = null;
        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f=new File(getCachePath());
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);

        String str = getTime()+"------------"+log;
        pw.println(str);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getTime(){
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        return time;
    }

}
